﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FieldDevice;
using System.Drawing.Imaging;
namespace FieldDevice.UI
{
    public partial class GraphMeter : UserControl
    {
        public GraphMeter()
        {
            InitializeComponent();
            this.GraphLocation = new Point(50, 50);
            this.GraphSize = new Size(200, 200);         
  
        }
       private Device _dev;

        
        public Device Device
        {
            get { return _dev; }
            set
            {
                this._dev = value;
                if (this._dev != null)
                    this._dev.ReadChangeEvent += new Device.ReadChangeEventHander(GraphMeter_ReadChangeEvent);
            }
        }

        public float value = 0.0f;

        void GraphMeter_ReadChangeEvent( Device device,string name, float value)
        {
            this.Refresh();
        }
        #region

        public Point GraphLocation { get; set; }
        public Size GraphSize { get; set; }
        private float xOffset;     //x轴偏移量
        private float yOffset;     //y轴偏移量
        private const int CountY = 11;   //y轴的直线数
        private const int CountX = 11;
        private  SolidBrush curveBoxBrush;
        #endregion



        #region   ///属性值
        [Category("GraphMeter"), Description("标题")]
        public string Title { get; set; }

        [Category("GraphMeter"),Description("X坐标名")]
        public string XAxisName { get; set; }

        [Category("GraphMeter"),Description("Y坐标名")]
        public string YAxisName { get; set; }

        [Category("GraphMeter")]
        public float XInterval { get; set; }

        [Category("GraphMeter")]
        public float XRangeMin { get; set; }

        [Category("GraphMeter")]
        public float XRangeMax { get; set; }

        [Category("GraphMeter"),Description("曲线框的宽度")]
        public float CurveBoxWidth 
        {
            get { return this.Height; }
        }

        [Category("GraphMeter"),Description("曲线框的高度")]
        public float CurveBoxHeight { get { return this.Width; }}

        [Category("GraphMeter"),Description("整个曲线框图的背景颜色")]
        public Color GraphBackColor { get; set;  }

        [Category("GraphMeter"), Description("内部曲线框图的背景颜色")]
        public Color InternalBackgroundColor { get; set; }

        [Category("GraphMeter"), Description("网格颜色")]
        public Color GridColor { get; set; }

        [Category("GraphMeter"), Description("字体颜色")]
        public Color FontColor { get; set; }

        [Category("GraphMeter"), Description("字体大小")]
         public FontFamily FontName { get; set; }
        [Category("GraphMeter"), Description("波形图1")]

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pen"></param>
        /// <param name="graphic"></param>
        /// <param name="arrayList"></param>

        public void DrawingValue(List<float> value, float maxValue, Color color, Graphics graph)
        {
            Graphics g = graph;
            int length = value.Count;
            Pen pen = new Pen(color);

            if (length < 30 && length >= 2)
            {
                for (int i = 1; i < length; i++)
                {
                    PointF pt1 = new PointF(this.Width * (30 - i) / 30, (maxValue - value[length - 1 - i]) * this.Height / maxValue);
                    PointF pt2 = new PointF(this.Width * (31 - i) / 30, (maxValue - value[length - i]) * this.Height / maxValue);
                    g.DrawLine(pen, pt1, pt2);
                }
            }
             if (length >= 30)
             { 
                for(int i=1;i<30;i++)
                 {
                    PointF pt1 = new PointF(this.Width * (30 - i) / 30, (maxValue - value[length - 1 - i]) * this.Height / maxValue);
                    PointF pt2 = new PointF(this.Width * (31 - i) / 30, (maxValue - value[length - i]) * this.Height / maxValue);
                    g.DrawLine(pen, pt1, pt2);
                 }
              }            
        }

        private void GraphMeter_Paint(object sender, PaintEventArgs e)
         {
             Graphics graph = e.Graphics;
             Pen gridPen = new Pen(GridColor, 0.01F);          //画虚线表示的网格线
             gridPen.DashStyle = DashStyle.Dot;
             Pen MediuPen = new Pen(GridColor, 0.01F);          //画用实线表示的网格线
             curveBoxBrush = new SolidBrush(GraphBackColor);
             xOffset = this.Width / (CountX + 1);
             yOffset = this.Height / (CountY + 1);
             //构建整个曲线框图区域
             graph.DrawRectangle(new Pen(Color.Black, 4.0F), 0, 0, this.Width, this.Height);
             //画横坐标
             for (int i = 1; i <= CountY; i++)
             {
                 if (i % 3 != 0)
                     graph.DrawLine(gridPen, 0, yOffset * i, this.Width, yOffset * i);
                 else
                     graph.DrawLine(MediuPen, 0, yOffset * i, this.Width, yOffset * i);
             }

             //画纵坐标
             for (int i = 1; i <= CountX; i++)
             {
                 if (i % 3 != 0)
                     graph.DrawLine(gridPen, xOffset * i, 0, xOffset * i, this.Height);
                 else
                     graph.DrawLine(MediuPen, xOffset * i, 0, xOffset * i, this.Height);
             }
            // graph.Dispose();
             gridPen.Dispose();
             MediuPen.Dispose();
             curveBoxBrush.Dispose();
         }


    }
}
